#include "scene.h"

namespace common
{
    Scene::Scene()
    {
        _root = new osg::Group;
        _viewer = new osgViewer::Viewer;
        _viewer->setSceneData( _root );
        createPlane();
    }
    Scene::~Scene()
    {

    }

    void Scene::createPlane()
    {
        osg::Geode *lpGeode = new osg::Geode;
        {
            osg::Geometry *lpGeometry = new osg::Geometry;


            osg::Vec3Array *lpVertexArray = new osg::Vec3Array;
            osg::Vec4Array *lpColorArray = new osg::Vec4Array;

            for ( int i = -100; i <= 100; i ++ )
            {
                unsigned int oldSize = lpVertexArray->size();
                for ( int j = -100; j <= 100; j ++ )
                {
                    lpVertexArray->push_back( osg::Vec3( i, j, 0.0 ) );
                    lpVertexArray->push_back( osg::Vec3( i + 1, j, 0.0 ) );
                }
                lpGeometry->addPrimitiveSet( new osg::DrawArrays ( osg::PrimitiveSet::TRIANGLE_STRIP, oldSize, lpVertexArray->size() - oldSize ) );
            }

            lpColorArray->push_back( osg::Vec4( 0.0, 1.0, 1.0, 0.3 ) );
            lpGeometry->setVertexArray( lpVertexArray );
            lpGeometry->setColorArray( lpColorArray, osg::Array::BIND_OVERALL );

            lpGeometry->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);

            lpGeometry->getOrCreateStateSet()->setMode(GL_BLEND,osg::StateAttribute::ON);
            lpGeometry->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);

            lpGeode->addDrawable( lpGeometry );
        }
        {
            osg::Geometry *lpGeometry = new osg::Geometry;


            osg::Vec3Array *lpVertexArray = new osg::Vec3Array;
            osg::Vec4Array *lpColorArray = new osg::Vec4Array;

            for ( int i = -100; i <= 100; i ++ )
            {
                lpVertexArray->push_back( osg::Vec3( i, -100, 0.0 ) );
                lpVertexArray->push_back( osg::Vec3( i, 100, 0.0 ) );

                lpVertexArray->push_back( osg::Vec3( -100, i, 0.0 ) );
                lpVertexArray->push_back( osg::Vec3( 100, i, 0.0 ) );

                //unsigned int oldSize = lpVertexArray->size();
                // for ( int j = -100; j <= 100; j ++ )
                // {
                //     lpVertexArray->push_back( osg::Vec3( i, j, 0.0 ) );
                //     lpVertexArray->push_back( osg::Vec3( i + 1, j, 0.0 ) );
                // }
                //lpGeometry->addPrimitiveSet( new osg::DrawArrays ( osg::PrimitiveSet::LINES, oldSize, lpVertexArray->size() - oldSize ) );
            }
            lpGeometry->addPrimitiveSet( new osg::DrawArrays ( osg::PrimitiveSet::LINES, 0, lpVertexArray->size() ) );

            lpColorArray->push_back( osg::Vec4( 0.0, 1.0, 1.0, 1.0 ) );
            lpGeometry->setVertexArray( lpVertexArray );
            lpGeometry->setColorArray( lpColorArray, osg::Array::BIND_OVERALL );

            lpGeometry->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
            //lpGeometry->getOrCreateStateSet()->setMode(GL_BLEND,osg::StateAttribute::ON);
            //lpGeometry->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);

            lpGeode->addDrawable( lpGeometry );
        }

        addNode( lpGeode );

        _plane = lpGeode;
    }

    void Scene::addNode( osg::Node *lpNode )
    {
        _root->addChild( lpNode );
    }

    void Scene::run()
    {
        _viewer->run();
    }
}
